﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using FileSystemControls;
using FileSystemControls.ListViewItems;

namespace FileTracker.Forms
{
    public partial class FormModifyDate : Form
    {
        List<FileListViewItem> _fileList;

        public FormModifyDate(List<FileListViewItem> FileList)
        {
            InitializeComponent();

            _fileList = FileList;
            dateTimePickerDate.Value = _fileList[0].ModifyDate;
            dateTimePickerTime.Value = _fileList[0].ModifyDate;

            if (_fileList.Count == 1)
            {
                checkBoxProgressive.Checked = false;
                checkBoxProgressive.Enabled = false;
            }

            listBoxFile.Items.AddRange(FileList.ToArray<FileListViewItem>());
        }

        private void buttonSave_Click(object sender, EventArgs argEventArgs)
        {
            DateTime NewDateTime = new DateTime(dateTimePickerDate.Value.Year,
                dateTimePickerDate.Value.Month,
                dateTimePickerDate.Value.Day,
                dateTimePickerTime.Value.Hour,
                dateTimePickerTime.Value.Minute,
                dateTimePickerTime.Value.Second,
                DateTimeKind.Local);

            //foreach (FileSystemListViewItem CurrentItem in _fileList)
            foreach (FileListViewItem CurrentItem in listBoxFile.Items)
            {
                if (CurrentItem is FileListViewItem)
                {
                    CurrentItem.SetCreateDate(NewDateTime);
                    if (checkBoxProgressive.Checked)
                    {
                        NewDateTime = NewDateTime.AddSeconds(1);
                    }
                }
            }
            this.Close();
        }

        private void buttonUp_Click(object sender, EventArgs argEventArgs)
        {
            int CurrentIndex;
            if (listBoxFile.SelectedIndex <= 0)
            {
                return;
            }

            CurrentIndex = listBoxFile.SelectedIndex;

            listBoxFile.Items.Insert(listBoxFile.SelectedIndex - 1, listBoxFile.SelectedItem);
            listBoxFile.Items.RemoveAt(CurrentIndex + 1);
            listBoxFile.SetSelected(CurrentIndex - 1, true);
        }

        private void buttonDown_Click(object sender, EventArgs argEventArgs)
        {
            int CurrentIndex;
            if ((listBoxFile.SelectedIndex < 0) || (listBoxFile.SelectedIndex >= listBoxFile.Items.Count - 1))
            {
                return;
            }

            CurrentIndex = listBoxFile.SelectedIndex;

            listBoxFile.Items.Insert(listBoxFile.SelectedIndex + 2, listBoxFile.SelectedItem);
            listBoxFile.Items.RemoveAt(CurrentIndex);
            listBoxFile.SetSelected(CurrentIndex + 1, true);
        }
    }
}
